﻿using System;
using System.Collections.Generic;

namespace OrderManager.Domain.Entities
{
    /// <summary>
    /// Represents customer entity of repository 
    /// </summary>
    public class Customer : EntityBase
    {
        /// <summary>
        /// Represents collection of orders for current customer
        /// </summary>
        private List<Order> _orders;

        /// <summary>
        /// Initialize a new instance of <see cref="OrderManager.Domain.Entities.Customer"/>
        /// </summary>
        protected Customer()
        {
        }

        /// <summary>
        /// Initialize a new instance of <see cref="OrderManager.Domain.Entities.Customer"/> with specifies id, name and address
        /// </summary>
        /// <param name="id">Customer's id</param>
        /// <param name="name">Customer's name</param>
        /// <param name="address">Customer's address</param>
        public Customer(Int32 id, string name, string address)
            : base(id)
        {
            Name = name;
            Address = address;

        }

        /// <summary>
        /// Represent name of current customer
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Represent address of current customer
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets a collection of orders for current customer (lazy)
        /// </summary>
        public List<Order> Orders
        {
            get
            {
                if (_orders == null)
                {
                    _orders = RepositoryFactory.GetOrderRepository().GetOrdersForCustomer(Id);
                }
                return _orders;
            }
        }

        /// <summary>
        /// Deletes current customer from repository
        /// </summary>
        public void Delete()
        {
            RepositoryFactory.GetCustomerRepository().Delete(Id);
        }

        /// <summary>
        /// Update repository entity with current customer properties values
        /// </summary>
        public void Save()
        {
            RepositoryFactory.GetCustomerRepository().Update(this);
        }
    }

}
